<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>选择收货地址</title>
  <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
  <meta content="yes" name="apple-mobile-web-app-capable"/>
  <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
  <meta content="telephone=no" name="format-detection"/>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="__STATIC__/plugs/layui/lay/modules/layer.js"></script>
</head>
<body>
  {if condition="!empty($address)"}
  <div id="address">
  {foreach name="address" item="v"}
    <div>
      <a href="javascript:;" onclick="select_address(this,{$v.id})">
      姓名：<span class="consignee">{$v.consignee}</span>
      电话：<span class="mobile">{$v.mobile}</span>
      收货地址：<p class="address">{$v.address}</p>
      </a>
    </div>
  {/foreach}
  </div>
  {else /}
  <div id="address">

  </div>
  {/if}

<input type="button" value="新增" onclick="add_address();">

<div id="add_address" style="display:none;">
  姓名：<input type="text" name="consignee" value=""><br/>
  电话：<input type="text" name="mobile" value=""><br/>
  详细地址：<input type="text" name="address" value=""><br/>
  邮编：<input type="text" name="zipcode" value=""><br/>
  设置默认：<input type="checkbox" name="is_default" value="1"><br/>
  <input type="button" value="提交" onclick="post_address()">
</div>


</body>
</html>
<script>
  /**
   *  显示添加板块
   */
  function add_address(){
      $('#add_address').css('display','block');
  }
  /**
   *  提交添加的地址
   */
  function post_address(){
      var consignee = $('input[name=consignee]').val();
      var mobile = $('input[name=mobile]').val();
      var address = $('input[name=address]').val();
      var zipcode = $('input[name=zipcode]').val();
      var is_default = $('input[name=is_default]').val();

      if(consignee == ''){
          alert('请填写收货地址');
      }
      if(mobile == ''){
          alert('请填写联系电话');
      }
      if(!(/^1[34578]\d{9}$/.test(mobile))){
          alert('联系电话格式不正确');
      }
      if(address == ''){
          alert('请填写收货地址');
      }

      $.post("{:url('GoodsList/get_address_list')}",
          {consignee:consignee,mobile:mobile,address:address,zipcode:zipcode,is_default:is_default},
          function(data){
              if(data.code == 1){
                  var html = '<div><a href="javascript:;" onclick="select_address(this,'+data.data.id+')">姓名：<span class="consignee">'+data.data.consignee+'</span>电话：<span class="mobile">'+data.data.mobile+'</span>收货地址：<p class="address">'+data.data.address+'</p></a></div>';
                  $('#address').append(html);
                  $('#add_address').css('display','none');
                  alert('保存成功');
                  //添加数据成功，关闭弹出窗之前，刷新列表页面的数据
                  /*parent.window.location.href=$('.domain_name').val()+"/Stat/QueryUserCategoryListStat.action";
                  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                  parent.layer.close(index);*/
              }else{
                  alert(data.msg);
              }
          }
      );
  }

  /**
   *  选择收货地址
   */
  function select_address(obj,id){
      var consignee = $(obj).find('.consignee').text();
      var mobile = $(obj).find('.mobile').text();
      var address = $(obj).find('.address').text();
      javascript:window.parent.select_address(id,consignee,mobile,address);
//
//
//
//      layer.close(index);
//
//
//      layer.close(index);
//      javascript:window.parent.call_back(id,name,score,remark,operater);
  }


</script>